package SwordFingerOffer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Q57_2_NSumEqualS {
    //有序序列 N 个数和为 S 输出所有序列
    // ==>三数之和有序版 no
    // ==>滑动窗口 ==> 难度在于 left right 的控制 ==> 消除影响
    public int[][] findContinuousSequence(int target) {
        if (target < 3){
            return new int[][]{};
        }
        int left = 0;
        int right = 0;
        //返回二维数组的话 泛型用数组
        List<int[]> ans = new ArrayList<>();
        int num = 0; //从1 开始
        while (right != target){
            // ... 一直累加
            if (num < target){
                num += ++right;
            }else if (num == target){
                //收集答案
                int i = left + 1;
                int[] tmp = new int[right - i + 1];
                for (int j = 0; j < tmp.length; j++) {
                    tmp[j] = i++;
                }
                ans.add(tmp);
                num =  num - ++left + ++right;
            }else {
                //... num > target
                num -= ++left;
            }
        }
        //返回二维数组的操作
        int[][] res = new int[ans.size()][];
        int index;
        for (int i = 0; i < res.length; i++) {
            res[i] = ans.get(i);
        }


        return res;
    }

    public static void main(String[] args) {
        Q57_2_NSumEqualS fun = new Q57_2_NSumEqualS();
        System.out.println(Arrays.deepToString(fun.findContinuousSequence(15)));
    }

}
